package com.ruoyi.swla2.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.swla2.domain.Terminals;
import com.ruoyi.swla2.domain.WarnStrategy;
import com.ruoyi.swla2.domain.Zone;
import com.ruoyi.swla2.service.ITerminalsService;
import com.ruoyi.swla2.service.IWarnStrategyService;
import com.ruoyi.swla2.service.IZoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 告警策略Controller
 *
 * @author gdqt
 * @date 2023-06-16
 */
@RestController
@RequestMapping("/params/strategyParams")
public class WarnStrategyController extends BaseController {
    @Autowired
    private IWarnStrategyService warnStrategyService;
    @Autowired
    private IZoneService zoneService;
    @Autowired
    private ITerminalsService terminalsService;

    /**
     * 查询告警策略列表
     */
    @PreAuthorize("@ss.hasPermi('params:strategyParams:list')")
    @GetMapping("/list")
    public TableDataInfo list(WarnStrategy warnStrategy) {
        startPage();
        List<WarnStrategy> list = warnStrategyService.selectWarnStrategyList(warnStrategy);
        list.forEach(ws -> this.doSetTargetName(ws));
        return getDataTable(list);
    }

    /**
     * 导出告警策略列表
     */
    @PreAuthorize("@ss.hasPermi('params:strategyParams:export')")
    @Log(title = "告警策略", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WarnStrategy warnStrategy) {
        List<WarnStrategy> list = warnStrategyService.selectWarnStrategyList(warnStrategy);
        ExcelUtil<WarnStrategy> util = new ExcelUtil<WarnStrategy>(WarnStrategy.class);
        util.exportExcel(response, list, "告警策略数据");
    }

    /**
     * 获取告警策略详细信息
     */
    @PreAuthorize("@ss.hasPermi('params:strategyParams:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        WarnStrategy ws = warnStrategyService.selectWarnStrategyByID(ID);
        this.doSetTargetName(ws);
        return success(ws);
    }

    /**
     * 新增告警策略
     */
    @PreAuthorize("@ss.hasPermi('params:strategyParams:add')")
    @Log(title = "告警策略", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WarnStrategy warnStrategy) {
        try {
            warnStrategy.setTenantId(this.getTenantId());
            warnStrategyService.insertWarnStrategy(warnStrategy);
        } catch (Exception e) {
            return error("已存在相同报警策略，请勿重复添加");
        }
        return success();
    }

    /**
     * 修改告警策略
     */
    @PreAuthorize("@ss.hasPermi('params:strategyParams:edit')")
    @Log(title = "告警策略", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WarnStrategy warnStrategy) {
        try {
            warnStrategyService.updateWarnStrategy(warnStrategy);
        } catch (Exception e) {
            return error("此次修改提交与现有报警策略冲突，请重新修改");
        }
        return toAjax(warnStrategyService.updateWarnStrategy(warnStrategy));
    }

    /**
     * 删除告警策略
     */
    @PreAuthorize("@ss.hasPermi('params:strategyParams:remove')")
    @Log(title = "告警策略", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(warnStrategyService.deleteWarnStrategyByIDs(IDs));
    }

    private void doSetTargetName(WarnStrategy ws) {
        if (ws.getIsGeneral() == 0) {
            if (ws.getTargetType().equals("4")) {
                Terminals terminals = terminalsService.selectTerminalsByTerminalId(ws.getTargetId());
                ws.setTargetName(terminals.getTerminalName());
            } else {
                Zone zone = zoneService.selectZoneByAreaId(ws.getTargetId());
                ws.setTargetName(zone.getAreaName());
            }
        }
    }
}
